package com.sq.partner.manager.controller;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.common.exception.AppException;
import com.sq.partner.manager.mybatis.model.GatewayStandbook;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.service.IGatewayChannelInfoConfigService;
import com.sq.partner.manager.service.IGatewayStandBookListService;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;
import com.sq.partner.manager.util.StrUtil;

/**
 * 
 * @author DingHao
 * 建行台账查询 /standBookList
 */
@Controller
@RequestMapping(value = "/standBookList")
public class StandBookListController {
	
	private static Logger logger = LogUtil.getLog(StandBookListController.class);
	
	@Resource
	private IGatewayStandBookListService standBookListService;
	
	@Resource
	private IGatewayChannelInfoConfigService channelInfoConfigService;
	
	
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(){
		return "transManager/standBookList";
	}
	


	
	/**
	 * 分页条件查询台账信息
	 * @param session
	 * @param pageIndex
	 * @param standbook
	 * @param privilegeBranch
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryStandBookPageByCriteria")
	@ResponseBody
    public Page queryStandBookPageByCriteria(HttpSession session, Integer pageIndex,
    			GatewayStandbook standbook,String privilegeBranch) throws Exception {
		
		Page page = beforeQueryStandBookPageByCriteria(session, pageIndex,  privilegeBranch,standbook);
		if("error".equals(page.getResult()) || "noChannelInfo".equals(page.getResult())){
			return page;
		}
		return standBookListService.queryStandBookPageByCriteria(page);
    }
	
	
	
	/**
	 * 台账查询前进行验证
	 * @param session
	 * @param pageIndex
	 * @param standbook
	 * @param privilegeBranch
	 * @return
	 * @throws Exception
	 */
	public Page beforeQueryStandBookPageByCriteria(HttpSession session, Integer pageIndex,
			String privilegeBranch,GatewayStandbook standbook) throws Exception{
		GatewayUser user = (GatewayUser)session.getAttribute(Constants.SESSION_USER);
		
		Page page = new Page();
		Map<String,Object> map = new HashMap<String, Object>();
		if(pageIndex != -1){
			page.setCurrent(pageIndex);
			map = SqlUtil.buildPageMap(page);
		}
		//机构验证
		if(StrUtil.isEmpty(privilegeBranch)){
			throw new AppException("系统异常：机构不能为空");
		}else{
			if(!(channelInfoConfigService.checkIsHasPrivilege(session, privilegeBranch))){
				throw new AppException("用户权限不足");
			}	
		}
	
		
	
		String endDate="",beginDate="";
		//保单开始结束日期
		if(StrUtil.isNotEmpty(standbook.getBeginDate())){
			beginDate = standbook.getBeginDate() + "00:00:00";
		}
		if(StrUtil.isNotEmpty(standbook.getEndDate())){
			endDate = standbook.getEndDate() + "23:59:59";
		}
		map.put("dptCde",privilegeBranch);
		map.put("polNo", standbook.getPolNo());
		map.put("sysFlag", standbook.getSysFlag());
		map.put("status", standbook.getStatus());
		map.put("bbrName", standbook.getBbrName());
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		page.setType(map);
		return page;
	}
	
	/**
	 * 查询当前用户的权限机构
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryPrivilegeBranch",method = RequestMethod.POST)
	@ResponseBody
    public Result queryPrivilegeBranch(HttpSession session) throws Exception {
		GatewayUser user=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		logger.info("当前登录用户的userCode为"+user.getUserCode()+",DepartmentStr:"+user.getDepartmentStr());
		
		if(null != user && StrUtil.isNotEmpty(user.getDepartmentStr())){
			return Result.data(standBookListService.queryPrivilegeBranch(user.getDepartmentStr()));
		}
		
		return Result.fail();
    }
	
	
	
}
